package com.exahq.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exahq.common.lang.Result;
import com.exahq.entity.Companydata;
import com.exahq.entity.Partnerinfo;
import com.exahq.service.PartnerinfoService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhq
 * @since 2021-02-06
 */
@RestController
@RequestMapping("/partnerinfo")
public class PartnerinfoController {
    @Autowired
    PartnerinfoService partnerinfoService;

    /**
     * 根据持股人查询
     * @param id
     * @return
     */
    @GetMapping("/byPidList/{id}")
    public Result byPidList(@PathVariable(name = "id") Integer id) {
        QueryWrapper<Partnerinfo> queryWrapper =new QueryWrapper<Partnerinfo>();
        System.out.println(id);
        queryWrapper.eq("pid",id);
        queryWrapper.eq("del_flag",1);
        List<Partnerinfo> list =partnerinfoService.list(queryWrapper);
        boolean flag=true;
        if (list.size()==0){
            flag =false;
        }
        return Result.succ(flag,list);

    }

    /**
     * 根据此股人查询企业相关信息
     * @param id
     * @return
     */
    @GetMapping("/byPidData/{id}")
    public Result byPidData(@PathVariable(name ="id") Integer id) {
        List<Partnerinfo> list =partnerinfoService.byPidData(id);
        System.out.println(list);
        boolean flag=true;
        if (list.size()==0){
            flag =false;
        }
        return Result.succ(flag,list);

    }

    /**
     * 带条件分页查询
     * @param query
     * @param response
     * @return
     */
    @PostMapping("/page")
    public Result page(@RequestBody Map<Object,Object> query, HttpServletResponse response) {

        List <Partnerinfo> list =partnerinfoService.selList(query);
        int totalCount = partnerinfoService.getCount(query);
        Map<Object,Object> map =new HashMap<Object,Object>();
        map.put("list",list);
        map.put("totalCount",totalCount);
       return Result.succ(map);
    }
    /***
     * 增加/修改
     * @param partnerinfo
     * @return
     */
    @PostMapping("saveEdit")
    public Result saveEdit(@RequestBody Partnerinfo partnerinfo){
        if (partnerinfo!=null && !partnerinfo.equals("")){
            System.out.println(partnerinfo);
            if (partnerinfo.getPInfoId()== null){
                partnerinfo.setUpDate(new Date());
                boolean b = partnerinfoService.save(partnerinfo);
                if (b){
                    return Result.succ("新添成功",b);
                }
            }else {
                partnerinfo.setUpDate(new Date());
                if (partnerinfoService.saveOrUpdate(partnerinfo))
                    return Result.succ("修改成功",partnerinfoService.saveOrUpdate(partnerinfo));
            }
        }
        return Result.succ(null);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public Result del(@PathVariable(name = "id") Integer id) {
        return Result.succ( partnerinfoService.delete(id));
    }

    /**
     *详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result detail(@PathVariable(name = "id") Integer id) {
        Partnerinfo partnerinfo = partnerinfoService.getById(id);
        return Result.succ(partnerinfo);
    }

    /**
     *企业持股人
     * @param companyId
     * @return
     */
    @RequestMapping(value = "byCidList",method = RequestMethod.POST)
    @PostMapping("/byCidList")
    public Result byCidList(@RequestBody String companyId) {
        JSONObject json_test = JSONObject.fromObject(companyId);
        System.out.println(companyId);
        List<Partnerinfo> list = partnerinfoService.byCidData(Integer.valueOf(json_test.get("cid").toString()));
        boolean flag=true;
        if (list.size()==0){
            flag =false;
        }
        return Result.succ(flag,list);

    }
}
